import Vue from 'vue';
import { codeMap } from "./codeMap";
/**
 * @description: 处理埋点公共数据
 * @params：接收非公共数据合并处理
 * @e:接收事件events对象
 * @return {*}
 */
class trackingClass {
    constructor(params, e = null) {
        this.params = params;
        this.events = e;
    }
    // 初始化数据同事重新组装数据返回
    initParams() {
        this.userInfo = Vue.prototype.$store.state.user.userInfo;
        this.basicParams = {
            brandId: this.userInfo.brandId,
            customerId: this.userInfo.customerId,
            openId: uni.getStorageSync('openid'),
            unionId: this.userInfo.unionId,
            channel: Vue.prototype.$store.getters['user/channelGetters'] || '1',//智肤新生公众号、智肤新生小程序;护心小爱公众号、护心小爱小程序
            dataSource: '小程序',//小程序、h5
            platform: '微信小程序'//微信小程序、微信公众号
        }
        if (Object.hasOwnProperty.call(this.params, 'eventParams')) {
            this.params.eventParams = this.params.eventParams.map(ele => {
                if (ele.key == 'kt_channel' && ele.value == '') ele.value = this.basicParams.channel;
                return ele;
            })
        }
        // 区分页面事件和点击事件需要的时间属性
        if (Object.hasOwnProperty.call(this.params, 'viewtime')) {
            this.params.leaveTime = Vue.prototype.$utils.datetime('Y-m-d h:i:s')
        } else {
            this.params.clickTime = Vue.prototype.$utils.datetime('Y-m-d h:i:s')
        }
        // 接收事件events对象统一处理位置坐标
        if (this.events && Object.hasOwnProperty.call(this.events, 'touches')) {
            this.params = Object.assign({}, this.params, {
                atElementXP: this.events.touches[0].clientX + '',
                atElementYP: this.events.touches[0].clientY + '',
                atPageXP: this.events.touches[0].pageX + '',
                atPageYP: this.events.touches[0].pageY + '',
            })
        }
        // 内部处理trackingCode对应[entityName:事件名称、事件类型],[actionId:事件code]
        if (Object.hasOwnProperty.call(this.params, 'trackingCode')) {
            const parser = codeMap.create(this.params['trackingCode']);
            this.basicParams = Object.assign({}, this.basicParams, parser)
        }
        console.log('内部',Vue.prototype.$store.getters['user/channelGetters']);
        return Object.assign(this.basicParams, this.params);
    }
    // 检查当前对象中是否有对应key
    checkInitParams(key) {
        let checkParams = Object.assign(this.basicParams, this.params);
        return Object.hasOwnProperty.call(checkParams, key)
    }
}

export { trackingClass }